package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.DealParam;
import cn.net.shuxing.yifu.common.bean.DealParamKey;
import cn.net.shuxing.yifu.common.mapper.DealParamMapper;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import org.simpleframework.xml.core.Complete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
public class DealParamService extends BaseService<DealParam, DealParamMapper> {

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private DealParamKeyService dealParamKeyService;

    @Autowired
    private RedisDataService redisDataService;

    public int insert(Integer userId, Integer coinTypeId, Integer dealParamKey, String dealParamValue) {
        DealParam dealParam = new DealParam();
        dealParam.setUserId(userId);
        dealParam.setCoinTypeId(coinTypeId);
        dealParam.setDealParamKey(dealParamKey);
        dealParam.setDealParamValue(dealParamValue);
        int result = super.insert(dealParam);

        DealParamKey dealParamKey1 = dealParamKeyService.getByPrimaryKey(dealParamKey);
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.lpush(dealParamKey1.getRedisSpecialUserListKey(), String.valueOf(userId));
        //jedisUtil.close();
        return result;
    }

    public int update(Integer userId, Integer coinTypeId, Integer dealParamKey, String dealParamValue) {
        DealParam dealParam = new DealParam();
        dealParam.setUserId(userId);
        dealParam.setCoinTypeId(coinTypeId);
        dealParam.setDealParamKey(dealParamKey);
        dealParam.setDealParamValue(dealParamValue);
        super.update(dealParam);
        return 1;
    }

    public String getValue(Integer userId, Integer coinTypeId, Integer dealParamKey) {
        DealParam dealParam = new DealParam();
        dealParam.clearCreateTimeAndUpdateTime();
        dealParam.setUserId(userId);
        dealParam.setCoinTypeId(coinTypeId);
        dealParam.setDealParamKey(dealParamKey);
        dealParam = mapper.selectOne(dealParam);
        if (dealParam != null)
            return dealParam.getDealParamValue();
        return null;
    }

    /**
     * 获取交易参数值,如果用户是特殊设置了的,就从表里取,否则使用默认值
     *
     * @param userId
     * @param coinTypeId
     * @param paramName  参数名
     * @return
     */
    public String getValue(Integer userId, Integer coinTypeId, String paramName) {
        String result = null;
        DealParamKey dealParamKey = dealParamKeyService.getByParamName(paramName);
        String defaultValueKey = dealParamKey.getRedisDefaultValueKey();
        String specialUserListKey = dealParamKey.getRedisSpecialUserListKey();
        //Jedis //jedis = jedisUtil.getJedis();
        List<String> specialUserIdList = jedisUtil.lrange(specialUserListKey, 0L, -1L);

        if (specialUserIdList.contains(String.valueOf(userId))) {
            /** 如果是特殊设置过的,就在数据库里查 **/
            Integer key = dealParamKeyService.getKeyByParamName(paramName);
            result = this.getValue(userId, coinTypeId, key);

        } else {
            /** 否则取redis里的默认值 **/
            result = jedisUtil.get(defaultValueKey);
            if (result == null) {
                result = redisDataService.get(defaultValueKey);
            }
        }
        //jedisUtil.close();
        return result;
    }

    public List<DealParam> getsByUserId(Integer userId) {
        DealParam dealParam = new DealParam();
        dealParam.clearCreateTimeAndUpdateTime();
        dealParam.setUserId(userId);
        return gets(dealParam);
    }

    @Override
    protected void complete(DealParam bean) {
        if (bean == null)
            return;
        String dealParamName = "";
        DealParamKey dealParamKey = dealParamKeyService.getByPrimaryKey(bean.getDealParamKey());
        if (dealParamKey != null) {
            dealParamName = dealParamKey.getDealParamName();
        }
        bean.setDealParamKeyName(dealParamName);
    }
}
